﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using PcProject.Application.Service.Products;
using PcProject.Domain.Models.Entities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace MiniPc.API.Controllers
{
    /// <summary>
    /// 商品规格控制器
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    [Authorize] //授权过滤
    public class SpecificaController : ControllerBase
    {
        /// <summary>
        /// 依赖注入
        /// </summary>
        private ISpecificaService _sepcService;
        public SpecificaController(ISpecificaService sepcService)
        {
            _sepcService = sepcService;
        }


        /// <summary>
        /// 显示商品规格
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetSepcifications()
        {
            List<Specification> lst = _sepcService.GetSpecifications();
            return Ok(new { data = lst, code = 0 });
        }

        /// <summary>
        /// 添加商品规格
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> AddSpecica(Specification obj)
        {
            try
            {
                var n = await _sepcService.AddSpecAsyn(obj);
                if (n > 0)
                {
                    return Ok(new { data = n, code = 1 });
                }
                else
                {
                    return Ok(new { data = n, code = 0 });
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }

        /// <summary>
        /// 删除商品规格
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpDelete]
        public async Task<IActionResult> DeleSpecica(int id)
        {
            try
            {
                int n = await _sepcService.DeleSpecAsync(id);
                if (n > 0)
                {
                    return Ok(new { code = 1 });
                }
                else
                {
                    return Ok(new { code = 0 });
                }
            }
            catch (Exception ex)
            {

                throw new Exception(ex.Message);
            }
        }

        /// <summary>
        /// 反填商品规格
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult FillSpecica(int id)
        {
            var list = _sepcService.FillSpecAsync(id);
            return Ok(new { data = list, code = 0 });
        }

        /// <summary>
        /// 修改商品规格
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> UptSpecica(Specification obj)
        {
            try
            {
                int n = await _sepcService.UptSpecAsync(obj);
                if (n > 0)
                {
                    return Ok(new { data = n, code = 1 });
                }
                else
                {
                    return Ok(new { data = n, code = 0 });
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }

    }
}
